package com.singzi.leetcode.N800;

public class T0794 {
    public static void main(String[] args) {
        String [] board = {"OXX","XOX","OXO"};
        Solution0794 solution0794 = new Solution0794();
        System.out.println(solution0794.validTicTacToe(board));
    }


}
class Solution0794 {
    private int []check = new int[2];
    public boolean validTicTacToe(String[] board) {
        if(numberCheck(board))
        return false;
        if(check[0]>=3||check[1]>=3){
            if(check[0]==check[1]){
                for(int i = 0;i < 3;i++){
                    boolean flag = true;
                    for (int j = 0; j < 3; j++) {
                        if (board[i].charAt(j) != 'X') {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) return false;
                }
                for(int i = 0;i < 3;i++){
                    boolean flag = true;
                    for (int j = 0; j < 3; j++) {
                        if (board[j].charAt(i) != 'X') {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) return false;
                }
                if(board[1].charAt(1)=='X'){
                    return (board[0].charAt(0) != 'X' || board[2].charAt(2) != 'X') && (board[2].charAt(2) != 'X' || board[2].charAt(0) != 'X');
                }
            }
            if(check[0]>check[1]){
                for(int i = 0;i < 3;i++){
                    boolean flag = true;
                    for (int j = 0; j < 3; j++) {
                        if (board[i].charAt(j) != 'O') {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) return false;
                }
                for(int i = 0;i < 3;i++){
                    boolean flag = true;
                    for (int j = 0; j < 3; j++) {
                        if (board[j].charAt(i) != 'O') {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) return false;
                }
                if(board[1].charAt(1)=='O'){
                    return (board[0].charAt(0) != 'O' || board[2].charAt(2) != 'O') && (board[2].charAt(2) != 'O' || board[2].charAt(0) != 'O');
                }

            }
        }
        return true;
    }
    public boolean numberCheck(String[] board){
        for (String s : board) {
            for(int i = 0;i< 3;i++){
                if(s.charAt(i)=='X') check[0]++;
                if(s.charAt(i)=='O') check[1]++;
            }
        }
        return Math.abs(check[0] - check[1]) > 1||(check[1]>check[0]);
    }
}